<?php
namespace app\company\controller;
use think\Controller;
use think\Db;
use think\Session;
class Xj extends Controller
{
    //宣讲列表页面
    public function xjlist()
    {
        $company_id = Session::get('company_id');
        $res = Db::name('preach')
        ->where('preach_status',0)
        ->where('company_id',$company_id)
        ->order('preach_id desc')
        ->select();
        //  dump($res);
        //  exit;
        $this->assign("res",$res);
        return $this->fetch();
    }
    //添加宣讲页面
    public function addxj()
    {
       return $this->fetch();
    }
    //处理添加宣讲信息
    public function addxj1()
    {
        $company_id = Session::get('company_id');
        $file = request()->file('preach_pic');
        // dump($file);
        // exit;
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'uploads');
            if($info){
                //赋值图片名字
                $preach_pic= $info->getFilename(); 
            }else{
                // 上传失败获取错误信息
                // echo $file->getError();
            }
        }
        $data = [
            'preach_name' =>input('preach_name'),
            'preach_site' =>input('preach_site'),
            'preach_beigin_time' => input('preach_beigin_time'),
            'preach_info' =>input('preach_info'),
            'company_id' =>$company_id,
            'preach_status' =>0,
            'preach_time' =>date('Y-m-d H:i:s',time()),
            'preach_pic' => $preach_pic,
            ];
        // dump($data);
        // exit;
        $res = Db::table('preach')->insert($data);
          if($res)
          {
              return $this->success('添加成功','xj/xjlist');
          }
          else
          {
            return $this->error('新增失败');
          }
    }
    //宣讲详情页面
    public function xjdetails($preach_id)
    {
        $company_id = Session::get('company_id');
        $res = Db::name('preach')
        ->where('preach_status',0)
        ->where('company_id',$company_id)
        ->where('preach_id',$preach_id)
        ->select();
        //  dump($res);
        //  exit;
        $this->assign("res",$res);
        return $this->fetch();
    }
    //处理宣讲信息修改
    public function updatexj($preach_id)
    {
        $file = request()->file('preach_pic');
        if($file == null)
        {
            $data = [
                'preach_name' =>input('preach_name'),
                'preach_site' =>input('preach_site'),
                'preach_beigin_time' => input('preach_beigin_time'),
                'preach_info' =>input('preach_info'),
                // 'company_id' =>$company_id,
                // 'preach_status' =>0,
                'preach_time' =>date('Y-m-d H:i:s',time()),
                // 'preach_pic' => $preach_pic,
                ];
            // dump($data);
            // exit;
            $res = Db::table('preach')->where('preach_id',$preach_id)->update($data);
              if($res)
              {
                  return $this->success('修改成功','xj/xjlist');
              }
              else
              {
                return $this->error('修改失败');
              }
        }
        else
        {
            if($file){
                $info = $file->rule('uniqid')->validate(['size' => 5242880, 'ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static' . DS .'uploads');
                if($info){
                    //赋值图片名字
                    $preach_pic= $info->getFilename(); 
                }else{
                    // 上传失败获取错误信息
                    // echo $file->getError();
                }
            }
            $data = [
                'preach_name' =>input('preach_name'),
                'preach_site' =>input('preach_site'),
                'preach_beigin_time' => input('preach_beigin_time'),
                'preach_info' =>input('preach_info'),
                // 'company_id' =>$company_id,
                // 'preach_status' =>0,
                'preach_time' =>date('Y-m-d H:i:s',time()),
                'preach_pic' => $preach_pic,
                ];
            // dump($data);
            // exit;
            $res = Db::table('preach')->where('preach_id',$preach_id)->update($data);
              if($res)
              {
                  return $this->success('修改成功','xj/xjlist');
              }
              else
              {
                return $this->error('修改失败');
              }
        }
    }
     //删除宣讲
     public function xjdelete($preach_id)
     {
         $data = [
           'preach_status'=>1,
         ];
         $res = Db::table('preach')->where('preach_id',$preach_id)->update($data);
         if($res){
             return $this->success('删除成功','xj/xjlist');
         }
         else{
           return $this->error('删除失败');
         }
     }
}